<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <!-- 移除 cycleType 表单项 -->

        <!-- 使用 row 包裹，实现一行两列 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="周期编码" prop="cycleCode">
              <el-input v-model="formData.cycleCode" placeholder="请输入周期编码" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="周期名称" prop="cycleName">
              <el-input v-model="formData.cycleName" placeholder="请输入周期名称" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="周期开始时间" prop="cycleStartDate">
              <el-date-picker clearable v-model="formData.cycleStartDate" type="date" value-format="timestamp"
                            placeholder="选择周期开始时间" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="周期结束时间" prop="cycleEndDate">
              <el-date-picker clearable v-model="formData.cycleEndDate" type="date" value-format="timestamp"
                            placeholder="选择周期结束时间" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="触发类型" prop="triggerType">
              <el-select v-model="formData.triggerType" placeholder="请选择触发类型" style="width: 100%">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.TRIGGER_TYPE)" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提前天数" prop="advanceDate">
              <el-input v-model.number="formData.advanceDate" type="number" placeholder="请输入提前天数" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="间隔周期" prop="invokeFrequency">
              <el-input v-model.number="formData.invokeFrequency" type="number" placeholder="请输入间隔周期" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行起始日期" prop="invokeBeginDate">
              <el-date-picker clearable v-model="formData.invokeBeginDate" type="date" value-format="timestamp"
                            placeholder="选择执行起始日期" style="width: 100%"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="执行起始时间" prop="invokeBeginTime">
              <el-date-picker clearable v-model="formData.invokeBeginTime" type="date" value-format="timestamp"
                            placeholder="选择执行起始时间" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="描述">
              <Editor v-model="formData.description" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as MaintenanceCycleApi from '@/api/devices/maintenancecycle';
import Editor from '@/components/Editor';
import { getDictDatas,getDictDataLabel, DICT_TYPE } from '@/utils/dict'

export default {
  name: "MaintenanceCycleForm",
  components: {
    Editor,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        cycleType: 1, // 默认 cycleType == 1
        cycleCode: undefined,
        cycleName: undefined,
        cycleStartDate: undefined,
        cycleEndDate: undefined,
        triggerType: undefined,
        advanceDate: undefined,
        invokeFrequency: undefined,
        invokeBeginDate: undefined,
        invokeBeginTime: undefined,
        description: undefined,
        status: 1, // 修改为数字类型，与表单控件值类型匹配
      },
      // 表单校验
      formRules: {
        status: [{required: true, message: '状态', trigger: 'blur'}],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await MaintenanceCycleApi.getMaintenanceCycle(id);
          this.formData = res.data;
          this.dialogTitle = "编辑设备保养周期";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增设备保养周期";
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await MaintenanceCycleApi.updateMaintenanceCycle(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await MaintenanceCycleApi.createMaintenanceCycle(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        cycleType: 1, // 默认 cycleType == 1
        cycleCode: undefined,
        cycleName: undefined,
        cycleStartDate: undefined,
        cycleEndDate: undefined,
        triggerType: undefined,
        advanceDate: undefined,
        invokeFrequency: undefined,
        invokeBeginDate: undefined,
        invokeBeginTime: undefined,
        description: undefined,
        status: 1, // 修改为数字类型，与表单控件值类型匹配
      };
      this.resetForm("formRef");
    }
  }
};
</script>
